package com.education.platform.management.controller.goods;

import com.education.platform.common.enums.DicTypeEnum;
import com.education.platform.common.exception.BusinessException;
import com.education.platform.common.util.BeanUtil;
import com.education.platform.common.util.StringUtils;
import com.education.platform.dao.entity.GoodsDO;
import com.education.platform.dao.entity.SysDictionaryDO;
import com.education.platform.management.enums.PushTypeEnum;
import com.education.platform.management.push.PushUtil;
import com.education.platform.management.service.GoodsService;
import com.education.platform.management.service.SysDictionaryService;
import com.education.platform.management.vo.request.GoodsQueryVO;
import com.education.platform.management.vo.request.GoodsUpdateVO;
import com.education.platform.management.vo.request.OnShelfVO;
import com.education.platform.management.vo.response.GoodsPageInfoVO;
import com.education.platform.management.vo.response.GoodsResponseVO;
import com.education.platform.management.vo.response.SysDictionaryResponseVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* describe: 商品
* @author : tomato
* @date: 2020-06-29 10:28:07.601
**/
@RestController
@Api("商品")
@RequestMapping("goods")
@Validated
public class GoodsController {

    private static final Logger LOGGER = LoggerFactory.getLogger(GoodsController.class);

	@Autowired
    private GoodsService goodsService;
    @Autowired
    private SysDictionaryService sysDictionaryService;
    @Autowired
    private PushUtil pushUtil;
    /**
     * 分页查询
     * @param goodsQueryVO 分页查询参数
     * @return 分页参数
     */
    @ApiOperation(value = "分页查询")
    @PostMapping("getPageList")
    public GoodsPageInfoVO selectPageList(@RequestBody @Valid GoodsQueryVO goodsQueryVO) {
        String info = String.format("The method name[selectPageList] params:%s", goodsQueryVO.toString());
        LOGGER.info(info);
        GoodsDO goods = BeanUtil.convert(goodsQueryVO, GoodsDO.class);

        Page<GoodsResponseVO> page = PageHelper.startPage(goodsQueryVO.getPageNum(), goodsQueryVO.getPageSize());
        List<GoodsDO> goodsList = goodsService.getList(goods);
        GoodsPageInfoVO goodsPageInfo = new GoodsPageInfoVO();
        BeanUtils.copyProperties(page.toPageInfo(), goodsPageInfo);
        goodsPageInfo.setPageNum(goodsQueryVO.getPageNum());
        List<GoodsResponseVO> voList = BeanUtil.convert(goodsList, GoodsResponseVO.class);
        goodsPageInfo.setList(voList);
        return goodsPageInfo;
    }


    @ApiOperation(value = "获取商品类型")
    @PostMapping("selectTypes")
    public List<SysDictionaryResponseVO> selectTags() {
        List<String> typeCodes = Arrays.asList(DicTypeEnum.GOODS_TYPE.getValue());
        List<SysDictionaryDO> dictionaryDTOS = sysDictionaryService.getListByDicTypeCodeList(typeCodes);
        List<SysDictionaryResponseVO> result = new ArrayList<>();
        result.addAll(BeanUtil.convert(dictionaryDTOS,SysDictionaryResponseVO.class));
        return result;
    }

    @ApiOperation(value = "新增/修改商品")
    @PostMapping("save")
    public void save(@RequestBody @Valid GoodsUpdateVO vo) {
        goodsService.save(BeanUtil.convert(vo,GoodsDO.class));

    }



    @ApiOperation(value = "上架")
    @PostMapping("onShelf")
    public void onShelf(@RequestBody OnShelfVO onShelfVO) {
        List<Integer> ids = onShelfVO.getIds();
        if(CollectionUtils.isEmpty(ids)){
            throw new BusinessException("数据为空");
        }
       List<GoodsDO> updateList = new ArrayList<>(ids.size());
        GoodsDO goodsDO;
        for(Integer id:ids){
            goodsDO = new GoodsDO();
            goodsDO.setId(id);
            goodsDO.setPublish(true);
            updateList.add(goodsDO);
        }
        goodsService.updateInBatch(updateList);
        //推送消息
        if(StringUtils.isNotEmpty(onShelfVO.getPushTitle())&&StringUtils.isNotEmpty(onShelfVO.getPushText())){
            for(Integer id:ids){
                pushUtil.push( PushTypeEnum.GOODS, String.valueOf(id),onShelfVO.getPushTitle(),onShelfVO.getPushText() );
            }
        }
    }
    @ApiOperation(value = "下架")
    @PostMapping("downShelf")
    public void downShelf(@RequestBody  List<Integer> ids) {
        if(CollectionUtils.isEmpty(ids)){
            throw new BusinessException("数据为空");
        }
        List<GoodsDO> updateList = new ArrayList<>(ids.size());
        GoodsDO goodsDO;
        for(Integer id:ids){
            goodsDO = new GoodsDO();
            goodsDO.setId(id);
            goodsDO.setPublish(false);
            updateList.add(goodsDO);
        }
        goodsService.updateInBatch(updateList);
    }
}
